package com.ssmTest.controller;

import com.ssmTest.domian.Student;
import com.ssmTest.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("stu")
public class StuController {
    @Autowired
    private StudentService stuService;

    @RequestMapping(value = "findAll",produces = "html/text;charset=utf-8")
    public ModelAndView findAll(){
        ModelAndView mv = new ModelAndView();
        List<Student> stuList = stuService.findAll();
        mv.addObject("stuList",stuList);
        mv.setViewName("list");

        return mv;
    }

    @RequestMapping("add")
    public String add(Student student){
        stuService.add(student);
        return "redirect:findAll.do";
    }

    @RequestMapping("findOne")
    public ModelAndView findOne(Integer id,ModelAndView mv){
        Student student = stuService.findOne(id);
        mv.addObject("student",student);
        mv.setViewName("update");
        return mv;
    }

    @RequestMapping("update")
    public String update(Student student){

        stuService.update(student);
        return "redirect:findAll.do";
    }

    @RequestMapping("delete")
    public String delete(Integer[] ids){
        for (Integer id : ids) {
            System.out.println(id);
        }
        stuService.delete(ids);

        return "redirect:findAll.do";
    }


}
